(function() {
    angular.module('app').component('wordsControllerWord', {
        templateUrl: 'app/controllers/words.controller/words.controller.word/words.controller.word.html',
        controller: wordsControllerWord,
        controllerAs: 'vm',
        bindings: {
            seen: '='
        }
    });

    wordsControllerWord.$inject = ['$rootScope', '$scope', 'wordsControllerWordService', 'util', '$timeout'];

    function wordsControllerWord($rootScope, $scope, wordsControllerWordService, util, $timeout) {

        var vm = this;

        vm.filter = {};

        vm.filterConfig = {
            category: {
                default:false
            },
            noBtn: true
        }

        vm.isHot = true;

        vm.getWords = {
            type: 'hot',
            category: ''
        }

        vm.detailCatName = '';

        vm.option = {
            title: {},
            tooltip: {
                formatter: function(params) {
                    // console.log(params);

                    var data = params.data;
                    // var str = '<div style = "width:120px;text-align: left;">' +  + '</div>';


                    var str = '<div style = "text-align: left;font-size:14px;"><div style="font-size:14px;padding:4px ;border-bottom:1px solid #ccc;">' + params.name + '</div>近30天销售额: ' + util.bigNumFormat(params.value) + '<br> 近30天销售量: ' + util.bigNumFormat(data.sellAmount) + '<br/> 宝贝数: ' + data.productNum + ' 件' + '<br/> 店铺数: ' + data.shopNum + ' 店' + '<br/> 更新时间: ' + data.date + '</div>';


                    return str;
                }
            },
            series: [{
                type: 'wordCloud',
                gridSize: 25,
                sizeRange: [14, 50],
                rotationRange: [0, 0],
                shape: 'pentagon', // Shape can be 'circle', 'cardioid', 'diamond', 'triangle-forward', 'triangle', 'pentagon', 'star'
                textStyle: {
                    normal: {
                        color: function() {
                            return 'rgb(' + [
                                Math.round(Math.random() * 160),
                                Math.round(Math.random() * 160),
                                Math.round(Math.random() * 160)
                            ].join(',') + ')';
                        }
                    },
                    emphasis: {
                        shadowBlur: 10,
                        shadowColor: '#333'
                    }
                },
                data: []
            }]
        }

        vm.chartClickInfo = {};



        vm.chartClick = _chartClick; //function

        vm.remove = _remove;//function

        $scope.$watch('vm.isHot', function(newValue, oldValue) {
            // console.log(newValue + " old:" + oldValue);


            if (angular.equals(newValue, oldValue)) {
                // console.log('vm.isHot is not change');
                return;
            }
            if (newValue) {
                vm.getWords.type = 'hot';

            } else {
                vm.getWords.type = 'growth';

            }
            // console.log(vm.getWords);
            if(vm.getWords.category == ''){
                return;
            }
            init(vm.getWords);

        });


        var clearFun = $rootScope.$on('GLOBAL_CATEGORY_CHANGE_EV', function(e, detailName, category) {
            // console.log(detailName + category);

            vm.detailCatName = detailName;


            vm.getWords.category = category;

            //vm.isHot = true;

            init(vm.getWords);


        });

        function init(json) {

            vm.chartClickInfo = {};
        

            $rootScope.$emit('echart_draw_line_loading_EV', 'words_word');

            wordsControllerWordService.getWord(json).then(function(data) {

                var option = angular.copy(vm.option);

                angular.forEach(data, function(value, key) {
                    option.series[0].data.push({
                        name: value.words,
                        value: value.sellMoney,
                        words: value.words,
                        shopNum: value.shopNum,
                        sellMoney: util.bigNumFormat(value.sellMoney),
                        productNum: value.productNum,
                        sellAmount: value.sellAmount,
                        date: util.dateFormat(value.date),
                        category: value.category
                    });
                });

                $rootScope.$emit('echart_draw_line_EV', option, 'words_word');

            }, function(err) {})
        }



        function _chartClick(params) {

            //vm.getWords.word = params.data.name;

            //$rootScope.$emit('',);
            $timeout(function(){
                $rootScope.$emit('words_controller_component_EV',vm.getWords,params.data.name);
                vm.seen = false;
            },1);
            


            // var data = params.data;console.log(data);
            // $timeout(function(){
            //  vm.chartClickInfo = data;
            // },10);

            // $rootScope.$broadcast('wordTable_show_EV', data);

        }


        function _remove(){

        }



    }
})();
